package com.xwaf.platform.web.controller.auth;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xwaf.platform.common.api.CommonResponse;
import com.xwaf.platform.common.api.constants.BaseConstants;
import com.xwaf.platform.common.api.entity.ELUser;
import com.xwaf.platform.common.web.BaseController;
import com.xwaf.platform.system.api.entity.auth.PermissionModule;
import com.xwaf.platform.system.api.entity.auth.PermissionTemplate;
import com.xwaf.platform.system.api.request.auth.DistributionTemplateRequest;
import com.xwaf.platform.system.api.service.auth.PermissionModuleService;
import com.xwaf.platform.system.api.service.auth.PermissionTemplateService;
import com.xwaf.platform.system.api.service.auth.UserPermissionService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * <p>
 * 基础业务:菜单权限等
 * <p>
 * 
 * @author 李聪 <br>
 * @email xwaf_love@yeah.net <br>
 * @since JDK 1.8<br>
 * @date 2020年1月2日 下午1:05:37 <br>
 * @see 无<br>
 *      Copyright (c) 2020, xwaf_love@yeah.net All Rights Reserved.<br>
 */
@RestController
@Api(tags = "菜单权限相关")
@RequestMapping(value = "/m/commAuth/")
public class CommonAuthController extends BaseController {

	@Autowired
	private PermissionModuleService permissionModuleService;
	@Autowired
	private PermissionTemplateService permissionTemplateService;
	@Autowired
	UserPermissionService userPermissionService;

	@ApiOperation(value = "所有的模块", notes = "所有的模块", httpMethod = "GET", hidden = true, produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "findAllModuleCode", produces = "application/json;charset=UTF-8")
	public CommonResponse findAllModuleCode() {
		List<String> list = permissionModuleService.findAllModuleCode();
		return CommonResponse.createCommonResponse(list);
	}

	@ApiOperation(value = "所有的模版权限", notes = "所有的模版权限", httpMethod = "GET", hidden = true, response = PermissionTemplate.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "toTemplate", produces = "application/json;charset=UTF-8")
	public CommonResponse toTemplate() {
		ELUser loginUser = getLoginUser();
		List<PermissionTemplate> permissionTemplates = permissionTemplateService.findTemplateAll(loginUser);
		return CommonResponse.createCommonResponse(permissionTemplates);
	}

	@ApiOperation(value = "员工权限信息", notes = "员工权限信息", httpMethod = "GET", response = PermissionModule.class, produces = MediaType.APPLICATION_JSON_VALUE)
	@GetMapping(value = "toDistributionTemplate", produces = "application/json;charset=UTF-8")
	public CommonResponse toDistributionTemplate() {
		ELUser loginUser = getLoginUser();
		List<PermissionModule> permissionModules = permissionModuleService.findDistributionTemplateByUserId(loginUser);
		return CommonResponse.createCommonResponse(permissionModules);
	}

	/**
	 * 
	 * <p>
	 * 向员工分配权限
	 * <p>
	 * 
	 * @author 李聪 <br>
	 * @email xwaf_love@yeah.net <br>
	 * @since JDK 1.8<br>
	 * @date 2020年1月3日 下午3:30:56 <br>
	 * @see 无<br>
	 * @param session
	 * @param request
	 * @param uid
	 *            员工id集合
	 * @param checkModules
	 *            模块权限code
	 * @param checkOperations
	 *            操作权限code
	 * @param checkFields
	 *            敏感字段code
	 * @return
	 * @throws Exception
	 * @demo <br>
	 */
	@ApiOperation(value = "向员工分配权限", notes = "向员工分配权限", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
	@PostMapping(value = "/distributionTemplate", produces = "application/json;charset=utf-8")
	public CommonResponse distributionTemplate(HttpServletRequest request,
			@RequestBody DistributionTemplateRequest distributionTemplateRequest) throws Exception {
		ELUser user = getLoginUser();
		boolean msg = userPermissionService.distributionTemplate(distributionTemplateRequest, user);
		if (msg) {
			return CommonResponse.createCommonResponse();
		} else {
			return CommonResponse.createCustomCommonResponse(BaseConstants.FAIL.getCode(), "分配权限失败!");
		}
	}

}
